<?php
namespace Components\User\Sha;

class Logic {
  protected function getPaginate($builder, $limit, $page) {
    return ['code' => 0, 'pager'=>(new \Phalcon\Paginator\Adapter\QueryBuilder([
      'builder' => $builder,
      'limit' => $limit,
      'page' => $page
    ])) -> getPaginate()];
  }

  public function pager($query) {
    $builder = (new \Phalcon\Mvc\Model\Query\Builder())
      -> from([
        'user' => __NAMESPACE__ . '\\Model'
      ])
      -> orderBy('user.id DESC');

    $res = $this -> getPaginate($builder, $query['limit'], $query['page']);
    $items = [];

    $walletFileds = [];
    foreach(Wallet\Constants::getByIndex('user_wallet_record_fields') as $filed) { $walletFileds[$filed] = 0; }

    $users = $res['pager'] -> items -> toArray();
    foreach($users as $k => $u) {
      $items[$k]['user'] = $u;

      if($u['pid'] == 0) {
        $items[$k]['p_user']['account'] = '无';
        $items[$k]['p_user']['name'] = '无';
      } else {
        $pUser = Model::findFirst([
          'conditions' => 'id = ' . $u['pid'],
          'columns' => 'account, name'
        ]);
        if(!$pUser) {
          $items[$k]['p_user']['account'] = '无';
          $items[$k]['p_user']['name'] = '无';
        } else {
          $items[$k]['p_user']['account'] = $pUser -> account;
          $items[$k]['p_user']['name'] = $pUser -> name;
        }
      }

      $wallet = Wallet\Model::findFirst('user_id = ' . $u['id']);
      if(!$wallet) {
        $items[$k]['user_wallet'] = $walletFileds;
      } else {
        $items[$k]['user_wallet'] = $wallet -> toArray();
      }
    }

    return ['code'=>0,'pager'=>[
			'items' => $items,
			'first' => $res['pager'] -> first,
			'before' => $res['pager'] -> before,
			'current' => $res['pager'] -> current,
			'last' => $res['pager'] -> last,
			'next' => $res['pager'] -> next,
			'total_pages' => $res['pager'] -> total_pages,
			'total_items' => $res['pager'] -> total_items,
			'limit' => $res['pager'] -> limit
		]];
  }

  public function create($data) {
    $data['login_password'] = Auth::getLoginPassword($data['login_password']);
    $data['safe_password'] = Auth::geSafePassword($data['login_password']);
    $data['last_update_time'] = $data['create_time'] = time();
    $data['status'] = 0;
    $data['star_level'] = 1;
    $data['is_get_level'] = 0;
    $data['is_center'] = 0;
    $res = (new Service) -> create($data);
    if($res['code'] != 0) { return $res; }

    return ['code'=>0,'msg'=>'注册成功'];
  }

  public function login($data) {
    $res = (new Service()) -> findFirst('account = "' . $data['account'] . '"');
    if($res['code'] != 0) { return ['code'=>-1,'msg'=>'账号或密码错误!']; }

    $validation = new Validation();

    $checkStatus = $validation -> checkStatus($res['model'] -> status);
    if($checkStatus['code'] != 0) { return $checkStatus; }

    $checkLoginPassword = $validation -> checkLoginPassword($data['login_password'], $res['model'] -> login_password);
    if($checkLoginPassword['code'] != 0) { return ['code'=>-1,'msg'=>'账号或密码错误!']; }

    return ['code'=>0,'msg'=>'登陆成功','model'=>$res['model']];
  }
}
